package com.cleargroup.magic.cleara.c;

import android.content.Context;

import com.cleargroup.magic.cleara.c.command.ProcessCommand;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;


/**
 * Created by Administrator on 2016/3/1 0001.
 */
public class CleanCooling {
    private static final String TAG = CleanCooling.class.getSimpleName();

    private static final String KEY_UPDATE_TASK = TAG + "FClean" + "UpdateTask";
    private static final int TIME_LIMIT_1 = 5 * 60 * 1000;
    private static final int TIME_LIMIT_2 = 10 * 60 * 1000;
    //private static final int TIME_LIMIT_3 = 10 * 60 * 1000;
    //private static final String KEY_PROCESS_UPDATE = "process_update";

    private static CleanCooling instance;
    private final Context context;
    private int cleanCount = 0;
    private long totalSize = 0;

    private static List<IGroup> cachedList = new ArrayList<>();
    private static Hashtable<String, Integer> cachedTable = new Hashtable<>();

    public  static List<IGroup> getCachedList() {
        return cachedList;
    }

    protected static void setCachedList(List<IGroup> list) {
        cachedList = list;
    }

    protected static Hashtable<String, Integer> getCachedTable() {
        return cachedTable;
    }

    protected static void setTable(Hashtable<String, Integer> table) {
        cachedTable = table;
    }

    private CleanCooling(Context context) {
        this.context = context;
    }

    public static CleanCooling get(Context context) {
        if(instance == null) {
            instance = new CleanCooling(context);
        }
        return instance;
    }


    public void setTotalSize(long totalSize){
        this.totalSize = totalSize;
    }

    public long getTotalSize(){
        return  totalSize;
    }

    public int getCleanCount() {
        return cleanCount;
    }

    public void setCleanCount(int cleanCount) {
        this.cleanCount = cleanCount;
    }

    public void increaseCleanCount() {
        setCleanCount(getCleanCount() + 1);
    }

    public Context getContext() {
        return context;
    }

    public boolean isCooling() {
        boolean case0 = getCleanCount() == 1;
        boolean case1 = hasCache() && !isTimeUp5Min();
        boolean case2 = !hasCache() && !isTimeUp10Min();
        boolean res = case0 || case1 || case2;
        return res;
    }

//    public boolean isProcessCooling() {
//        boolean res= !BatteryHelper.isTimeUp(getContext(), KEY_PROCESS_UPDATE, TIME_LIMIT_3);
//        return res;
//    }

    public boolean hasCache() {
        boolean res = getCachedList() != null
                && getCachedList().size() > 0;
        return res;
    }

    private boolean isTimeUp5Min() {
        boolean res = BatteryHelper.isTimeUp(getContext(), KEY_UPDATE_TASK, TIME_LIMIT_1);
        return res;
    }

    private boolean isTimeUp10Min() {
        boolean res= BatteryHelper.isTimeUp(getContext(), KEY_UPDATE_TASK, TIME_LIMIT_2);
        return res ;
    }

    public void resetTimeUp() {
        BatteryHelper.resetTimeUp(getContext(), KEY_UPDATE_TASK);
    }

//    public void resetProcessTimeUp() {
//        SharedPreferences sp = context.getSharedPreferences(BatteryHelper.SP_NAME, Context.MODE_PRIVATE);
//        long current = System.currentTimeMillis();
//        Logger.d(TAG,current + "," + sp.getLong(KEY_PROCESS_UPDATE,0));
//        if(current - sp.getLong(KEY_PROCESS_UPDATE,0) > TIME_LIMIT_3){
//            sp.edit().putLong(KEY_PROCESS_UPDATE, current).commit();
//        }
//    }

    public void cancelCooling() {
        BatteryHelper.setTimeUp(getContext(), KEY_UPDATE_TASK, -1);
    }

    //移除已经不存在的文件
    public static void refreshData(){
        int groupSize = cachedList.size();
        for(int i=groupSize-1;i>=0;i--){
            IGroup group = cachedList.get(i);
            if(group.getKey().equals(ProcessCommand.TAG)){
                continue;
            }
            int childSize = group.getChildCount();
            for(int j=childSize-1;j>=0;j--){
                String key = group.getChild(j).getKey();
                if(key.equals("key_system_cache")){
                    continue;
                }else{
                    File file = new File(key);
                    if(file == null || !file.exists()) {
                        group.removeChild(j);
                    }
                }
            }
            if(group.getChildCount() == 0){
                cachedTable.remove(group.getKey());
                cachedList.remove(i);
            }

        }
    }

}
